<?php

class RssInfoManager
{

    public static function GetTableName()
    {
        return Util::GetDatabaseTableName("rss_info");
    }

    public static function GetAllSourceInfo()
    {
        global $wpdb;
        $tableName = Util::GetDatabaseTableName("rss_info");
        $sql = "SELECT * FROM $tableName;";
        $data = $wpdb->get_results($sql, ARRAY_A);
        $allInfo = array();
        foreach ($data as $dataRow)
        {
            $allInfo[] = self::CreateRssInfo($dataRow);
        }
        return $allInfo;
    }

    public static function GetRssInfoById( $id )
    {
        $sql = "SELECT * FROM rss_info WHERE rss_info_id = %s LIMIT 1";
        $data = Core::DBConn()->Query( $sql, Util::Quote($id) )->GetAssoc();
        $result = ( !empty($data) ) ? self::CreateRssInfo($data) : null;
        return $result;
    }

    public static function Save( RssInfo $rssInfo )
    {
        $tableName = self::GetTableName();
        $sql = "UPDATE $tableName
        		SET
        		  last_modified_date = @last_modified_date,
        		  title = @title,
        		  description = @description,
        		  link = @link
        		WHERE rss_info_id = @rss_info_id";
        $rssInfoId = $rssInfo->GetRssInfoId();
        $params = array(
            '@last_modified_date' => $rssInfo->GetLastModifiedDate(),
            '@title' => $rssInfo->GetTitle(),
            '@description' => $rssInfo->GetDescription(),
            '@link' => $rssInfo->GetLink(),
            '@rss_info_id' => $rssInfoId
        );

        Core::DBConn()->Query( $sql, Util::QuoteArray($params) );

        $rssPosts = $rssInfo->GetPosts();
        foreach ( $rssPosts as $rssPost )
        {
            $rssPost->SetRssInfoId($rssInfoId);
            RssPostManager::Save($rssPost);
        }
    }

    public static function InsertUrl( $url )
    {
        $tableName = self::GetTableName();
        global $wpdb;
        $sql = $wpdb->prepare("INSERT INTO $tableName (rss_source_url) VALUE (%s)", $url);
        Core::DBConn()->Query( $sql );
        return Core::DBConn()->GetLastInsertId();
    }

    public static function Insert( RssInfo $rssInfo )
    {
        $sql = 'INSERT INTO rss_info
                            (source_id, last_modified_date, title, description, link)
                        VALUE
                            (@source_id, @last_modified_date, @title, @description, @link)';

        $params = array
        (
            '@source_id' => $rssInfo->GetSourceId(),
            '@last_modified_date' => $rssInfo->GetLastModifiedDate(),
            '@title' => $rssInfo->GetTitle(),
            '@description' => $rssInfo->GetDescription(),
            '@link' => $rssInfo->GetLink()
        );

        Core::DBConn()->Query( $sql, Util::QuoteArray($params) );

        $id = Core::DBConn()->GetLastInsertId();

        $rssPosts = $rssInfo->GetPosts();
        foreach ( $rssPosts as $rssPost )
        {
            $rssPost->SetRssInfoId($id);
            RssPostManager::Insert($rssPost);
        }

        $rssInfo->SetRssInfoId($id);

        return $id;
    }

    public static function UpdateUrl( $newUrl, $rssInfoId )
    {
        $tableName = self::GetTableName();
        global $wpdb;
        $sql = $wpdb->prepare("UPDATE $tableName SET rss_source_url = %s WHERE rss_info_id = %s", $newUrl, $rssInfoId);
        Core::DBConn()->Query( $sql );
    }

    public static function Update( RssInfo $rssInfo )
    {
        $sql = 'UPDATE rss_info
        		SET
        		  source_id = @source_id,
        		  last_modified_date = @last_modified_date,
        		  title = @title,
        		  description = @description,
        		  link = @link
        		WHERE rss_info_id = @rss_info_id';

        $params = array(
            '@source_id' => $rssInfo->GetSourceId(),
            '@last_modified_date' => $rssInfo->GetLastModifiedDate(),
            '@title' => $rssInfo->GetTitle(),
            '@description' => $rssInfo->GetDescription(),
            '@link' => $rssInfo->GetLink(),
            '@rss_info_id' => $rssInfo->GetRssInfoId()
        );

        Core::DBConn()->Query( $sql, Util::QuoteArray($params) );

        return $rssInfo->GetRssInfoId();
    }

    public static function Delete( $rssInfoId )
    {
        $tableName = self::GetTableName();
        $sql = "DELETE FROM $tableName WHERE rss_info_id = %s";
        Core::DBConn()->Query( $sql, Util::Quote($rssInfoId) );
    }

    private static function CreateRssInfo( $data )
    {
        $rssInfo = new RssInfo();

        $rssInfo->SetSourceUrl($data['rss_source_url']);
        $rssInfo->SetTitle($data['title']);
        $rssInfo->SetDescription($data['description']);
        $rssInfo->SetLastModifiedDate($data['last_modified_date']);
        $rssInfo->SetLink($data['link']);
        $rssInfo->SetRssInfoId($data['rss_info_id']);

        return $rssInfo;
    }
}

?>